package com.szt;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * * Created by admin on 2018/1/12.
 * 
 * @author wwy
 */
public class Demo {
	private static ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(20000);
	private static Demo instance = new Demo();
	private static List<ArrayBlockingQueue<String>> list = new ArrayList<>();
	private static int count = 0;
	
	private Demo() {
	}
	
	/** * 单例函数 * * @return */
	public static Demo getInstance() {
		
		return instance;
	}
	
	public synchronized void send(String prtNo) {
		if (queue.size() < 5) {
			queue.add(prtNo);
		} else {
			ArrayBlockingQueue<String> q = queue;
			printAll(q);
			queue = new ArrayBlockingQueue<String>(20000);
			queue.add(prtNo);
		}
		
	}
	
	public synchronized void post(ArrayBlockingQueue q) {
		printAll(q);
	}
	
	private synchronized static void printAll(ArrayBlockingQueue q) {
		String value;
		Iterator iter = q.iterator();
		if (q.size() < 5) {
			System.out.println("数据量小于5：" + q);
		}
		while (iter.hasNext()) {
			value = (String) iter.next();
			System.out.print(value + ", ");
		}
		list.add(q);
		count++;
		System.out.println("已发送数据：" + list + "被调用" + count + "次");
	}
}
